package com.xiao.sulinguser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.sulingpublic.utils.R;
import com.xiao.sulinguser.entity.MemberEntity;
import com.xiao.sulinguser.myenum.RedisEnum;
import com.xiao.sulinguser.service.LoginService;
import com.xiao.sulinguser.service.MemberService;
import com.xiao.sulinguser.vo.UserQQVo;
import com.xiao.sulinguser.vo.UserWeiBoVo;
import com.xiao.sulinguser.vo.UserWeiXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/13 21:08
 * @作用: 登入相关功能 接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private MemberService memberService;

    /**
     * 验证本地账号登入 请求
     */
    @PostMapping("/verify")
    public R Login(String username,String password){

        MemberEntity member = loginService.verify(username, password);
        if(null != member){
            return R.ok().put("data",member);
        }else {
            return R.ok().put("data", RedisEnum.LOGIN_FAIL.getCode());
        }
    }

    /**
     * QQ登入时,发过来的数据, 如果第一次登入就保存一下用户信息
     * @param userQQVo
     * @return
     */
    @RequestMapping("/other/qq")
    public R otherLogin(@RequestBody UserQQVo userQQVo){
        MemberEntity member = null;

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();

        if(null != userQQVo){
            wrapper.eq("qq_id", userQQVo.getOpenId());
            member = memberService.getOne(wrapper);
            // 没有数据注册
            if(null == member){
                member = qqLogin(userQQVo);
                memberService.save(member);
            }
        }
        return R.ok().put("member", member);
    }
    /**
     * QQ登入时,发过来的数据, 如果第一次登入就保存一下用户信息
     * @param userWeiXinVo
     * @return
     */
    @RequestMapping("/other/weixin")
    public R otherLogin(@RequestBody UserWeiXinVo userWeiXinVo){
        MemberEntity member = null;

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        if(null != userWeiXinVo){
            wrapper.eq("weixin_id", userWeiXinVo.getOpenId());
            member = memberService.getOne(wrapper);
            // 没有数据注册
            if(null == member){
                member = weiXinLogin(userWeiXinVo);
                memberService.save(member);
            }
        }
        return R.ok().put("member", member);
    }
    /**
     * QQ登入时,发过来的数据, 如果第一次登入就保存一下用户信息
     * @param userWeiBoVo
     * @return
     */
    @RequestMapping("/other/weibo")
    public R otherLogin(@RequestBody UserWeiBoVo userWeiBoVo){
        MemberEntity member = null;

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        if(null != userWeiBoVo){
            wrapper.eq("weibo_id", userWeiBoVo.getIdstr());
            member = memberService.getOne(wrapper);
            // 没有数据注册
            if(null == member){
                member = weiBoLogin(userWeiBoVo);
                memberService.save(member);
            }
        }

        return R.ok().put("member", member);
    }


    /**
     * 将qq的数据模型转换为本地的对象
     * @return
     */
    private MemberEntity qqLogin(UserQQVo userQQVo){
        MemberEntity member = new MemberEntity();

        String substring = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        member.setUsername(substring);
        member.setNickname(userQQVo.getNickname());
        member.setHeader(userQQVo.getFigureurl_qq());
        member.setCity(userQQVo.getCity());
        int gender = userQQVo.getGender().contains("男") ?1:0;
        member.setGender(gender);
        member.setCreateTime(new Date());

        member.setQqId(userQQVo.getOpenId());

        return member;
    }
    /**
     * 将微信的数据模型转换为本地的对象
     * @return
     */
    private MemberEntity weiXinLogin(UserWeiXinVo userWeiXinVo){
        MemberEntity member = new MemberEntity();

        String substring = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        member.setUsername(substring);
        member.setNickname(userWeiXinVo.getNickName());
        member.setHeader(userWeiXinVo.getAvatarUrl());
        member.setCity(userWeiXinVo.getCity() + userWeiXinVo.getProvince());
        member.setGender(userWeiXinVo.getGender());
        member.setCreateTime(new Date());

        member.setWeixinId(userWeiXinVo.getOpenId());

        return member;
    }
    /**
     * 将微博的数据模型转换为本地的对象
     * @return
     */
    private MemberEntity weiBoLogin(UserWeiBoVo userWeiBoVo){
        MemberEntity member = new MemberEntity();

        String substring = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        member.setUsername(substring);
        member.setNickname(userWeiBoVo.getName());
        member.setHeader(userWeiBoVo.getProfile_image_url());
        member.setGender(userWeiBoVo.getGender().contains("m") ? 1:0);
        member.setCreateTime(new Date());

        member.setWeiboId(userWeiBoVo.getIdstr());

        return member;
    }
}
